<template>
  <base-dialog
    :title="title + '短信模板'"
    ref="baseDialog"
    @handleClose="handleClose"
    @handleSubmit="handleSubmit"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="135px">
      <el-form-item label="短信渠道编号" prop="channelId">
        <el-select v-model="form.channelId" placeholder="请选择短信渠道编号">
          <el-option
            v-for="channel in channelList"
            :key="channel.id"
            :value="channel.id"
            :label="channel.signature"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="短信类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择短信类型">
          <el-option
            v-for="item in typeList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="模板编号" prop="code">
        <el-input v-model="form.code" placeholder="请输入模板编号" />
      </el-form-item>
      <el-form-item label="模板名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入模板名称" />
      </el-form-item>
      <el-form-item label="模板内容" prop="content">
        <el-input
          type="textarea"
          v-model="form.content"
          clearable
          :autosize="{ minRows: 2, maxRows: 12 }"
          placeholder="请输入模板内容"
        />
      </el-form-item>
      <el-form-item label="开启状态" prop="status">
        <el-radio-group v-model="form.status">
          <el-radio label="Y">开启</el-radio>
          <el-radio label="N">关闭</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="短信 API 模板编号" prop="apiTemplateId">
        <el-input v-model="form.apiTemplateId" placeholder="请输入短信 API 的模板编号" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="form.remark"
          type="textarea"
          placeholder="请输入备注"
          show-word-limit
          clearable
          :autosize="{ minRows: 2, maxRows: 12 }"
          :maxlength="200"
        />
      </el-form-item>
    </el-form>
  </base-dialog>
</template>

<script>
import { add, edit } from "@/api/message/sms/template"

export default {
  name: "Dialog",
  props: {
    channelList: {
      default: () => [],
      type: Array
    },
    typeList: {
      default: () => [],
      type: Array
    }
  },
  data() {
    return {
      title: "",
      form: {},
      rules: {
        type: [{ required: true, message: "短信类型不能为空", trigger: "change" }],
        status: [{ required: true, message: "开启状态不能为空", trigger: "blur" }],
        code: [{ required: true, message: "模板编码不能为空", trigger: "blur" }],
        name: [{ required: true, message: "模板名称不能为空", trigger: "blur" }],
        content: [{ required: true, message: "模板内容不能为空", trigger: "blur" }],
        apiTemplateId: [
          { required: true, message: "短信 API 的模板编号不能为空", trigger: "blur" }
        ],
        channelId: [{ required: true, message: "短信渠道编号不能为空", trigger: "change" }]
      }
    }
  },
  methods: {
    init(item, title) {
      this.title = title
      this.$refs.baseDialog.dialogVisible = true
      if (item) {
        this.form = JSON.parse(JSON.stringify(item))
      }
    },
    changeHTML(val) {
      this.form.content = val
    },
    handleClose() {
      this.form = {}
      this.$refs.baseDialog.dialogVisible = false
      this.title = ""
    },
    // 提交表单
    handleSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.title === "编辑") {
            try {
              await edit(this.form)
              this.$message({
                type: "success",
                message: "短信模板编辑成功"
              })
              this.$emit("updatePage")
              this.handleClose()
            } catch (error) {
              this.$message({
                type: "error",
                message: "短信模板编辑失败"
              })
            }
          } else {
            try {
              await add(this.form)
              this.$message({
                type: "success",
                message: "短信模板新增成功"
              })
              this.$emit("updatePage")
              this.handleClose()
            } catch (error) {
              this.$message({
                type: "error",
                message: "短信模板新增失败"
              })
            }
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select,
.picker,
.el-input-number {
  width: 100%;
}
</style>
